/**
 * Packages used to run the program.
 */
import gameobjects.Game;
import gameobjects.Player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Author: Gunnar Sigurðsson.
 * Date: 11/21/12
 * Time: 7:53 PM
 */
public final class TicTacToe {

    /**
     * Main function to start the game.
     * @param args not used
     * @throws IOException if input is wrong throws IOException
     */
    public static void main(final String[] args) throws IOException {
        new TicTacToe();
    }

    /**
     * Constructor for TicTacToe.
     */
    private TicTacToe() {
        // Print welcome message.
        System.out.println("********************************");
        System.out.println("*    Welcome to TIC TAC TOE    *");
        System.out.println("********************************\n");

        try {
            // Intialize a new reader for console input.
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(System.in));

            System.out.println("Do you want to play Tic Tac Toe? Y for yes "
                    + "N for no");
            char playOrNot = 'N';
            try {
                playOrNot = reader.readLine().charAt(0);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (Character.toUpperCase(playOrNot) != 'Y') {
                System.exit(0);
            }

            // Ask for and read in player names.

            System.out.println("Name of Player 1: ");
            String player1 = reader.readLine();
            System.out.println("Name of Player 2: ");
            String player2 = reader.readLine();
            System.out.println();

            // Create new Player objects using the given names.
            Player p1 = new Player(player1, 'X');
            Player p2 = new Player(player2, 'O');

            // Create a new Game Object passing it the player
            // objects (or just players).
            Game newGame = new Game(p1, p2, new InputStreamReader(System.in));
            newGame.play(); // Start the game.
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
